home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / sorting.swg / 0061_Combsort with wrapper.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  1.2 KB  |  52 lines

  1. { Pascal example of COMBSORT with wrapper. }
  2.  
  3. program combsorttest;
  4.   { Pascal implementation of modified bubble sort or combsort.
  5.     combsort algorithm published 4-91 in Byte by Steven Lacey and
  6.     Richard Box
  7.  
  8.     Ported from COBOL source by Glenn Grotzinger }
  9.  
  10.   const
  11.     irgap: real = 1.3;
  12.   var
  13.     a: array[1..20] of integer;
  14.     swapvalue: integer;
  15.     swapnumber: integer;
  16.     i: integer;
  17.     jumpsize, tablesize, upperlimit: integer;
  18.     swap: boolean;
  19.  
  20.   begin
  21.     randomize;
  22.     for i := 1 to 20 do
  23.       a[i] := random(15);
  24.     for i := 1 to 20 do
  25.       write(a[i], '  ');
  26.     writeln;
  27.  
  28.     swap := true;
  29.     tablesize := 20;
  30.     jumpsize := tablesize;
  31.     repeat
  32.       jumpsize := trunc(jumpsize/irgap);
  33.       swap := true;
  34.       for i := 1 to (tablesize-jumpsize) do
  35.         begin
  36.           swapnumber := i + jumpsize;
  37.           if a[i] > a[swapnumber] then
  38.             begin
  39.               swapvalue := a[i];
  40.               a[i] := a[swapnumber];
  41.               a[swapnumber] := swapvalue;
  42.               swap := false;
  43.             end;
  44.         end;
  45.     until (swap) and (jumpsize <= 1);
  46.  
  47.     for i := 1 to 20 do
  48.       write(a[i], '  ');
  49.     writeln;
  50.   end.
  51.  
  52.